#!/usr/bin/bash

PRGNAME=acolor
VERSION=${VERSION:-1.1.3}
BUILD=${BUILD:-1}
TAG=${TAG:-Sqy}

set -e

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi


if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLK_GNAT_FLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLK_GNAT_FLAGS="-O2 -fPIC"
else
  SLK_GNAT_FLAGS="-O2"
fi

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=${PKG:-$TMP/package-$PRGNAME}
OUTPUT=${OUTPUT:-/tmp}

rm -rf $PKG $TMP/$PRGNAME-$VERSION
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
tar xvf $CWD/$PRGNAME-$VERSION.tar.xz
cd $PRGNAME-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

make SLK_GNAT_FLAGS="$SLK_GNAT_FLAGS"

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/doc/$PRGNAME-$VERSION
cat $CWD/$PRGNAME.SlackBuild > $PKG/usr/doc/$PRGNAME-$VERSION/$PRGNAME.SlackBuild
cp README $PKG/usr/doc/$PRGNAME-$VERSION
cp $PRGNAME  $PKG/usr/bin
strip  $PKG/usr/bin/$PRGNAME

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG/usr/bin
ln -s $PRGNAME color

cd $PKG
makepkg -l y -c n ${OUTPUT}/$PRGNAME-$VERSION-$ARCH-${BUILD}${TAG}.${PKGTYPE:-txz}
